TARGET=gameripper

SRCS=$(wildcard *.c) $(wildcard engines/*.c)
OBJS=$(SRCS:.c=.o)
CFLAGS=-ansi -pedantic -Wall -Wextra -Wno-stringop-truncation -s -I. -I.. -I./SDL-SDL2/include -I./libwebp-main/src -I./libjpeg
ifeq ($(DEBUG),)
 CFLAGS+=-O3 -ffast-math -msse4
else
 CFLAGS+=-g -DDEBUG=1
endif
ifneq ("$(wildcard /bin/*.exe)","")
OS=windows
EXT=.exe
OBJS+=resource.o
CFLAGS+=-Wno-long-long
LDFLAGS+=-mwindows -static-libgcc
LIBS+=-Wl,--nxcompat -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive \
    -luser32 -lcomdlg32 -lole32 -lshlwapi -lws2_32 -loleaut32 -lwinmm -limm32 -lsetupapi -luuid -lversion -lm
else
OS=linux
EXT=
LDFLAGS+=-pthread
LIBS+=-lm -ldl
endif

ALIBS=SDL-SDL2/build/.libs/libSDL2.a libwebp-main/src/libwebp.a libwebp-main/src/demux/libwebpdemux.a libwebp-main/sharpyuv/libsharpyuv.a \
	libjpeg/libjpeg.a

all: $(TARGET)

libsize:
	@du -hs `find . -type f -name "*.a"`

SDL-SDL2/autogen.sh:
	wget https://github.com/libsdl-org/SDL/archive/refs/heads/SDL2.zip
	@rm -rf SDL-SDL2 2>/dev/null || true
	@unzip SDL2.zip
	@rm SDL2.zip
	@printf "#ifndef SDL_dynapi_h_\n#define SDL_dynapi_h_\n#define SDL_DYNAMIC_API 0\n#endif" >SDL-SDL2/src/dynapi/SDL_dynapi.h
	@touch SDL-SDL2/autogen.sh

SDL-SDL2/build/.libs/libSDL2.a: SDL-SDL2/autogen.sh
ifeq ($(wildcard SDL-SDL2/configure),)
	@cd SDL-SDL2 && ./autogen.sh
	@# patch buggy configure script because "--disable-dsound" doesn't work, and SDL_OpenAudioDevice would use dsound
	@# which in turn would call ole32.dll.CoIncrementMTAUsage which is not implemented yet in latest wine as a built-in.
	@# And we don't want to need the real ole32.dll with wine, WINMM and WASAPI is more than enough for our needs.
	@cp SDL-SDL2/configure SDL-SDL2/configure.tmp
	@cat SDL-SDL2/configure.tmp | sed 's/have_dsound=yes/have_dsound=no/g' >SDL-SDL2/configure
endif
ifeq ($(wildcard SDL-SDL2/Makefile),)
	@cd SDL-SDL2 && CFLAGS="-O3 -ffast-math -msse4 -ffunction-sections -fdata-sections" ./configure --disable-shared --enable-static \
	--disable-dbus --disable-joystick --disable-haptic --disable-hidapi --disable-sensor --disable-power --disable-filesystem \
	--disable-file --disable-misc --disable-locale --disable-diskaudio --disable-hidapi-joystick --disable-hidapi-libusb \
	--disable-rpath --disable-joystick-virtual --disable-directsound --disable-dsound
endif
	@make --no-print-directory -s -C SDL-SDL2 all

libwebp-main/makefile.unix:
	wget https://github.com/webmproject/libwebp/archive/refs/heads/main.zip
	@rm -rf libwebp-main 2>/dev/null || true
	@unzip main.zip
	@rm main.zip
	@touch libwebp-main/makefile.unix

libwebp-main/src/libwebp.a: libwebp-main/makefile.unix
	make --no-print-directory -s -C libwebp-main -f makefile.unix src/libwebp.a src/demux/libwebpdemux.a sharpyuv/libsharpyuv.a

libjpeg/makefile.ansi:
	wget https://www.ijg.org/files/jpegsr9f.zip
	@rm -rf libjpeg 2>/dev/null || true
	@unzip jpegsr9f.zip
	@mv jpeg-9f libjpeg
	@rm jpegsr9f.zip
	@cp etc/jconfig.h libjpeg
	@touch libjpeg/makfile.ansi

libjpeg/libjpeg.a: libjpeg/makefile.ansi
	make --no-print-directory -s -C libjpeg -f makefile.ansi libjpeg.a

resource.o:
	windres -i etc/resource.rc -o resource.o

zlib.o: zlib.c
	$(CC) -ansi -O3 -c -o zlib.o zlib.c

%: %.c
	$(CC) $(CFLAGS) $< -c $@

$(TARGET): $(ALIBS) $(OBJS)
	$(CC) $(LDFLAGS) $(OBJS) -o $(TARGET) $(ALIBS) $(LIBS) -Wl,--gc-sections
ifeq ($(DEBUG),)
	@strip $(TARGET)$(EXT)
	@$(CC) etc/bin2c.c -o bin2c -lz
	@./bin2c -d
	@rm bin2c
endif

clean:
	rm $(TARGET)* *.o engines/*.o 2>/dev/null || true

zip: $(TARGET)
	cp $(TARGET)$(EXT) ..
	cd .. && zip gameripper-$(OS).zip README.md $(TARGET)$(EXT) && rm $(TARGET)$(EXT)

distclean: clean
	rm -rf SDL-SDL2 libwebp-main libjpeg
	rm ../gameripper-$(OS).zip

.PHONY: alibs
